<template>
  <div class="login-container">
    <div class="banner">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
               label-position="left">
        <div class="top" style="text-align: center">
          <img src="../../assets/logo.jpg" alt="" style="display: inline-block;border-radius: 50%;width: 80px;vertical-align: middle;margin-bottom: 14px;">
          <h3 class="title" style="line-height: 50px;height: 50px;vertical-align: middle">登陆</h3>
        </div>

        <el-form-item prop="username">

          <el-input v-model="loginForm.username" name="username" type="text" auto-complete="on" placeholder="用户名" class="black">

            <template slot="prepend">
              <span class="svg-container">
                <svg-icon icon-class="user"/>
              </span>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">

          <el-input
            :type="pwdType"
            v-model="loginForm.password"
            name="password"
            auto-complete="on"
            placeholder="密码"
            @keyup.enter.native="handleLogin">
            <template slot="prepend">
              <span class="svg-container">
                <svg-icon icon-class="password"/>
              </span>
            </template>
          </el-input>
          <span class="show-pwd" @click="showPwd">
          <svg-icon icon-class="eye"/>
        </span>
        </el-form-item>
        <el-form-item style="margin-top: 20px">
          <el-button type="text" style="float: right">
            <router-link to="retrieve" class="btn-link edit-btn">
              忘记密码？
            </router-link>
          </el-button>
          <el-button :loading="loading" type="primary" style="width:100%;background-color: #3ea751;border-color: #3ea751; margin-left: 0" @click.native.prevent="handleLogin">
            登陆
          </el-button>
          <!--<span style=" width: 100%;text-align: center;display: block;line-height: 20px;">or</span>-->
          <el-button   style="width:100%;background: #2b2b36;position: relative;height: 40px;margin: 10px auto;" @click.native.prevent=""  >
            <router-link to="register" class="btn-link edit-btn" style="position: absolute;width: 100%;height: 100%;left: 0;top: 0;line-height: 40px;">
              注册
            </router-link>
          </el-button>
        </el-form-item>
      </el-form>
      <div class="clearboth" style="clear: both"></div>
    </div>
    <div class="copyright" style="text-align: center;color: #FFF"><span>浙ICP备17056522号-1  copyright 2017-2019 E招招聘</span></div>
  </div>
</template>

<script>
  // import {isvalidUsername} from '@/utils/validate'

  export default {
    name: 'Login',
    data() {
      // const validateUsername = (rule, value, callback) => {
      //   if (!isvalidUsername(value)) {
      //     callback()
      //   } else {
      //     callback()
      //   }
      // }
      const validatePhone = (rule, value, callback) => {
        const reg = new RegExp('^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$')
        if (!value) {
          return callback(new Error('用户名不能为空!'))
        } else {
          callback()
        }
      }
      const validatePass = (rule, value, callback) => {
        if (value.length < 5) {
          callback(new Error('密码不能小于5位!'))
        } else {
          callback()
        }
      }
      return {
        loginForm: {
          username: '',
          password: ''
        },
        loginRules: {
          username: [{required: true, trigger: 'blur', validator: validatePhone}],
          password: [{required: true, trigger: 'blur', validator: validatePass}]
        },
        loading: false,
        pwdType: 'password',
        redirect: undefined
      }
    },
    watch: {
      $route: {
        handler: function (route) {
          this.redirect = route.query && route.query.redirect
        },
        immediate: true
      }
    },
    methods: {
      showPwd() {
        if (this.pwdType === 'password') {
          this.pwdType = ''
        } else {
          this.pwdType = 'password'
        }
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            this.$store.dispatch('Login', this.loginForm).then(() => {
              this.loading = false
              this.$store.dispatch('GetCityAll')
              this.$router.push({path: '/'})
            }).catch(() => {
              this.loading = false
            })
          } else {
            return false
          }
        })
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $bg: #2d3a4b;
  $light_gray: #eee;
  .banner {
    /*background: #080818 url(https://res.wx.qq.com/mpres/zh_CN/htmledition/pages/login/loginpage/images/bg_banner412d83.png);*/
    left: 0;
    right: 0;
    width: 100%;
    padding: 35px 35px 15px 35px;
    margin: 12vw auto;
    background-repeat: no-repeat;
  }
  .black .el-input-group__prepend{
    background-color: #2b2b36;
    border: 1px solid #9595ad;
  }

  .el-form-item__content {
    line-height: normal;
    margin: 2px 0;
  }
  .el-input-group__prepend{
    background-color: #2b2b36;
    border: 1px solid #9595ad;
  }
  .el-input__inner{
    background-color: #2b2b36;
    border: 1px solid #9595ad;
  }

</style>

<style rel="stylesheet/scss" lang="scss" scoped>
  $bg: #2d3a4b;
  $dark_gray: #889aa4;
  $light_gray: #eee;
  .login-container {
    position: fixed;
    height: 100%;
    width: 100%;
    background: $bg  url("../../../static/images/bg1.jpg") center/100% 100% no-repeat;
    .login-form {
      width: 400px;
      padding: 35px 35px 15px 35px;
      background: #2b2b36;
      position: relative;
      margin: auto;
      /*float: right;*/
      /*margin-right: 20%;*/
      /*height: 290px;*/
      /*margin-bottom: 100px;*/
      border-radius: 5px;
    }
    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;
      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }
    .svg-container {
      color: $dark_gray;
      vertical-align: middle;
      display: inline-block;
    }
    .title {
      font-size: 26px;
      font-weight: 400;
      color: #dee4e7;
      margin: 0px auto 10px auto;
      text-align: center;
      font-weight: bold;

    }
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
  }
</style>
